function setErrorClass() {
    $("input.error").attr("class","textError");
    $("select.error").attr("class","selectError");
    $("textarea.error").attr("class","textareaError");
}

function resetErrorClass() {
    resetFormHints();
    $("input").attr("class","text");
    $("select").attr("class","text");
    $("textarea").attr("class","text");

    $("input[type=submit]").attr("class","myButton");
    $("input[type=reset]").attr("class","myButton");
}

function customVal(validators){
	
}

function customValHead(validators){
	
}

function errorOnLabels(formNode, validators) {
	// Reset all the labels
	resetFormHints();
    setErrorClass();
	 //appele la function de validations propres au form
	 customVal(validators);
	 customValHead(validators);
	for(var i=0; i<validators.length; i++){
		var s = validators[i].name;
		var i1 = s.indexOf("[");
		if(i1 != -1) {
			var i2 = s.indexOf("]");
			id = s.substring(0, i1) + "_" + s.substring(i1+1,i2);
		} else {
			var i2 = s.indexOf("_");
			if(i2 != -1) {
				id = s;
			}else{
				id = s;
				switch(s) {
					case 'passwordPwd':
						id = 'inscription_password';
						break;
					case 'passwordPwdBis':
						id = 'inscription_passwordbis';
						break;
				}
			}
		}
		var lbl = getLinkedLabel(id);
		if(lbl){
			x= lbl.parent().find(".formHint");
			x.css("color", "#FF0000");
			x.html(validators[i].message);
		}
	}
	if(validators.length>0){
		scroll(0,0);
		if(document.getElementById("errorDisplayHaut")) {
				document.getElementById("errorDisplayHaut").style.display = "block";
				document.getElementById("errorDisplayHaut").innerHTML = '<ul class="error">Certains champs du formulaire sont vides ou contiennent des erreurs.</ul>';
		}
		
	}
	if (validators.length == 0){
	    var allInputs = $(":input");
	    var formChildren = $("form > *");
	    for ( keyVar in allInputs ) { 
	    	   if(allInputs[keyVar].disabled){
	    		   allInputs[keyVar].disabled = false;
	    	   }
	    }
	}
}

function getLinkedLabel(fieldID) {
	var x = $(".formHint").parent().find("label");
	for(var i=0; i<x.length; i++) {
		if(x.eq(i).attr("for") == fieldID) {
			return x.eq(i);
		}
	}
}

function resetFormHints() {
	var x = $(".formHint");
	for(var i=0; i<x.length; i++) {
		x.css("color", "#181B2A");
		x.html("&nbsp;");
	}
}
function displayError(formNode, validators){
    setErrorClass();
	 //appele la function de validations propres au form
	customVal(validators);
	customValHead(validators);
	var errorHTML = "";
	for(var i=0; i < validators.length; i++){
		//errorHTML += "<li><em>" + validators[i].name + '</' + 'em>: ' + validators[i].message + "</li>";
		errorHTML += "<li>" + validators[i].message + '<' + '/li>';
	}
	if(validators.length != 0) {
		if(document.getElementById("errorDisplayHaut")) {
			document.getElementById("errorDisplayHaut").style.display = "block";
			document.getElementById("errorDisplayHaut").innerHTML = '<ul class="error">' + errorHTML + "</ul>";
		}
		if(document.getElementById("errorDisplayBas")) {
			document.getElementById("errorDisplayBas").style.display = "block";
			document.getElementById("errorDisplayBas").innerHTML = '<ul class="error">' + errorHTML + "</ul>";
		}
	}
	if (validators.length == 0){
	    var allInputs = $(":input");
	    var formChildren = $("form > *");
	    for ( keyVar in allInputs ) { 
	    	   if(allInputs[keyVar].disabled){
	    		   allInputs[keyVar].disabled = false;
	    	   }
	    }
	}	
}

function popupError(formNode, validators){
	var errorHTML = validators[0].message;
    alert(errorHTML);
}

function textCounter(field, cntfield, maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	else
		cntfield.value = maxlimit - field.value.length;
}

function fOnChangePieceJointe () {
	// affiche le nom du fichier image car on ne le voit pas bien dans le champ

	/*
	var x = this.value;
	alert(x);
	alert(this.name);
    var n = 'idRappel' + this.name;
    alert(n);
    */
	document.getElementById('idRappelPieceJointe').innerHTML = document.getElementById('idPieceJointe').value;
}

function verifdt(dateavalider){
	
	verdat=dateavalider;//document.getElementById("demandes3_nom_guichet").value;
	/*datej= new Date();
	anneej=datej.getFullYear()+"*";
	anneej=anneej.substring(2,0);

	if (verdat.length ==6) 
	verdat=verdat.substring(2,0)+"/"+verdat.substring(4,2)+"/"+anneej+verdat.substring(6,4);
	*/
	if (verdat.length ==10){
		//verdat=verdat.substring(2,0)+"/"+verdat.substring(4,2)+"/"+verdat.substring(8,4);
		
		if(!isValidDate(verdat))
		    //alert("la date n'est pas valide ou n'est pas au bon format.\n format : jjmmaa ou jjmmaaaa ou jj/mm/aaaa");
			return false;
		else
			//dateavalider.value = verdat;
			return true;
	}else {
		return false;
	}

	
}

function isValidDate(d) {
	var dateRegEx = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
	return d.match(dateRegEx);
} 

// fonction de validation adresses mail
function bonmail(mailteste)

{
	var reg = new RegExp('^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$', 'i');

	if(reg.test(mailteste))
	{
		return(true);
	}
	else
	{
		return(false);
	}
}

function verifierDNS(dns, type){
	 formatDns = true;
	 if(dns.substr(0,1)!="K"){
		 formatDns = false;
	 }
	 switch (type){
		 case "pos":
			 if(dns.substr(3,2)!="20" && dns.substr(3,2)!="21"){
				 formatDns = false;
		 	 }
			 break;

		 case "imp":
			 if(dns.substr(3,2)!="22"){
				 formatDns = false;
		 	 }
			 break;
		 case "ser":
			 if(dns.substr(3,2)!="23"){
				 formatDns = false;
		 	 }
			 break;
	 }
	 if(dns.length!=8){
		 formatDns = false;
	 }
	 return(formatDns);
}
/******************************************************************
# NOM : Verifier_Numero_Telephone()
# BUT : Verifier le format d'un numero de telephone (01|02|03|04|05|06|08) + suite de chiffres
# E : Le numero de telephone
# S : Booleen
# COM :
# *******************************************************************/
function Verifier_Numero_Telephone(num_tel)
{
// Definition du motif a matcher
	var regex = new RegExp("^0[1-689][0-9]{8}$", "g");
// Test sur le motif
	if(regex.test(num_tel))
	{
		return(true);
	}
	else
	{
		return(false);
	}
} 

function Verifier_CodePostal(codepos)
{
// Definition du motif a matcher
	result = true;
	
	if(codepos.length != 5)
	{
		result = false;
	}else{
		/*Test numeric*/
		var regex = new RegExp("[0-9]", "g");
		if(!regex.test(codepos)){
			result = false;
		}
	}
	return(result);
} 
/*
 * 
 * FONCTION TRIM, LTRIM, RTRIM
 * SUPPRIME TOUT LES ESPACES EN DEBUT ET EN FIN DE CHAINE
 * 
 */
function trim(str, chars)
{
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars)
{
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) 
{
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}